package cn.zmit.kuxi.activity;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xdroid.functions.holder.ListViewDataAdapter;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;
import com.xdroid.widget.loadmore.LoadMoreContainer;
import com.xdroid.widget.loadmore.LoadMoreHandler;
import com.xdroid.widget.loadmore.LoadMoreListViewContainer;

import android.os.Bundle;
import android.widget.ListView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.entity.JoinUserEntity;
import cn.zmit.kuxi.holder.JoinUserHolder;
import cn.zmit.kuxi.request.RequestTask;

/** * @author chenxu @parameter * @return */
public class JoinUserActivity extends BaseActivity {
	@ViewInject(R.id.listView_win_record)
	private ListView list_goods;
	private ListViewDataAdapter<JoinUserEntity> joinUserAdapter;
	/**
	 * 页码
	 */
	private int mPage = 1;
	@ViewInject(R.id.load_more_list_view_container)
	private LoadMoreListViewContainer mLoadMoreListViewContainer;
	String gp_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitleView("参与用户", true);
		setContentView(R.layout.activity_winning_record);
		gp_id = getTextFromBundle("gp_id");
		ViewUtils.inject(this);
		initView();
		getDate();
		initLoadMore();
		
	}

	private void getDate() {
		RequestTask.getInstance().getNowBuyRecord(this, gp_id, mPage + "",
				new onRequestListener());
	}

	/**
	 * 初始化上拉刷新，分页显示
	 */
	private void initLoadMore() {
		mLoadMoreListViewContainer.useDefaultFooter(); // 使用默认的FooterView
		mLoadMoreListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {

			@Override
			public void onLoadMore(LoadMoreContainer loadMoreContainer) {
				if (!mLoadMoreListViewContainer.isLoadError()) {
					mPage++;
				}
				LogUtils.i("加载更多--->page:" + mPage);
				getDate();
			}
		});
	}

	// 请求
	private class onRequestListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			// TODO Auto-generated method stub
			super.onDone(request, response, dataType);
			Boolean mIsEmpty = false; // 是否是空数据
			Boolean mHasMore = false; // 是否有更多数据待加载
			JsonData jsonData = JsonData.create(response);

			if (jsonData != null && jsonData.length() > 0) {
				JsonData data = jsonData.optJson("user_list");
				if (data.length() > 0) {
					if (data.length() == 12) {
						mHasMore = true; // 有更多数据
					} else if (data.length() < 12) {
						mIsEmpty = false; // 有数据
						mHasMore = false; // 无更多数据
					}
					System.out.println("data.length()====" + data.length());
					for (int i = 0; i < data.length(); i++) {
						JsonData itemData = data.optJson(i);
						String joinNum = itemData.optString("buy_count");
						String userName = itemData.optString("name");
						String userPhone = itemData.optString("phone");
						String ip = itemData.optString("ip");
						String buyTime = itemData.optString("buyOn");
						String avatar = itemData.optString("avatar");
						String user_id = itemData.optString("user_id");
						JoinUserEntity joinUserEntity = new JoinUserEntity(
								avatar, userName, userPhone, joinNum, buyTime,
								ip,user_id);
						joinUserAdapter.append(joinUserEntity);
					}

				} else {
					// ToastUtils.show(getActivity(), "请求失败");
					mIsEmpty = true; // 空数据
					mHasMore = false; // 无更多数据
				}
				mLoadMoreListViewContainer.loadMoreFinish(mIsEmpty, mHasMore); // 判断是否加载更多

			}
		}

		@Override
		public void onRequestPrepare() {
			// TODO Auto-generated method stub
			super.onRequestPrepare();
		}

		@Override
		public void onRequestFailed(String error) {
			// TODO Auto-generated method stub
			super.onRequestFailed(error);
		}
	}

	private void initView() {
		joinUserAdapter = new ListViewDataAdapter<JoinUserEntity>();
		joinUserAdapter.setViewHolderClass(this, JoinUserHolder.class);
		list_goods.setAdapter(joinUserAdapter);

	}
}
